package com.lc.cehui.ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.request.AddressChangeApi;
import com.lc.cehui.http.request.AddressDelApi;
import com.lc.cehui.http.request.AddressListApi;
import com.lc.cehui.http.request.MyStaffListApi;
import com.lc.cehui.http.response.AddressBean;
import com.lc.cehui.http.response.RegisterBean;
import com.lc.cehui.http.response.StaffListBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.util.SPUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 地址管理界面
 */
public final class AddressListActivity extends AppActivity {
    @BindView(R.id.rl_title_bar)
    TitleBar rlTitleBar;
    @BindView(R.id.addr_recycle)
    RecyclerView mRecyclerView;
    @BindView(R.id.addr_num)
    TextView addrNum;
    @BindView(R.id.rl_status_refresh)
    TwinklingRefreshLayout refreshLayout;
    private int mPage;

    private BaseQuickAdapter adapter;
    private RelativeLayout rl_add_address;
    private List<AddressBean.DataBean> list = new ArrayList<AddressBean.DataBean>();
    private String flag;

    @Override
    protected int getLayoutId() {
        return R.layout.address_list_activity;
    }

    @Override
    protected void initView() {
        rlTitleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {
                startActivity(InsertAddrActivity.class);
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        adapter = new BaseQuickAdapter<AddressBean.DataBean, BaseViewHolder>(R.layout.item_address_manage, list) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, AddressBean.DataBean bean) {
                TextView name = (TextView) baseViewHolder.getView(R.id.tv_name);
                TextView phone = (TextView) baseViewHolder.getView(R.id.tv_phone);
                TextView addr = (TextView) baseViewHolder.getView(R.id.tv_address);
                name.setText(bean.getNames());

                phone.setText(bean.getMobile());
                addr.setText(bean.getProvince()+bean.getCity()+bean.getDistrict()+bean.getAddress()+bean.getRoom());
if (bean.getIs_check()==2){
    baseViewHolder.setImageResource(R.id.img_check,R.mipmap.add_true);
}else {
    baseViewHolder.setImageResource(R.id.img_check,R.mipmap.add_false);
}
                baseViewHolder.getView(R.id.edit_addr).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(AddressListActivity.this,InsertAddrActivity.class).putExtra("id",bean.getAddress_id()+"").putExtra("name",bean.getNames()).putExtra("mobile",bean.getMobile()).putExtra("province",bean.getProvince()).putExtra("city",bean.getCity()).putExtra("type","1").putExtra("district",bean.getDistrict()).putExtra("address",bean.getAddress()).putExtra("room",bean.getRoom()).putExtra("is_check",bean.getIs_check()+""));

                    }
                });
                baseViewHolder.getView(R.id.rl_check).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       if (bean.getIs_check()==1){
                           address_change(bean.getAddress_id()+"");
                       }
                    }
                });
                baseViewHolder.getView(R.id.del_addr).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        address_del(bean.getAddress_id()+"");
                    }
                });
            }
        };
        mRecyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                mPage = 1;
                list.clear();
                address_list();

                refreshLayout.finishRefreshing();

            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                refreshLayout.finishLoadmore();
//                if (!TextUtils.isEmpty(goods_classify_id)) {



                if (flag.equals("T")) {
                    mPage++;
                    address_list();
                } else {
                    refreshLayout.setEnableLoadmore(false);

                }
//         }
            }
        });
    }

    private void address_del(String id) {
        EasyHttp.post(this)
                .api(new AddressDelApi().setArticle_id(id).setUserid(SPUtil.get(this, IntentKey.USERID,0)))
                .request(new HttpCallback<RegisterBean>(this) {

                    @Override
                    public void onSucceed(RegisterBean data) {
                        list.clear();
                        address_list();
                    }
                });
    }

    private void address_change(String id) {
        EasyHttp.post(this)
                .api(new AddressChangeApi().setArticle_id(id).setUserid(SPUtil.get(this, IntentKey.USERID,0)))
                .request(new HttpCallback<RegisterBean>(this) {

                    @Override
                    public void onSucceed(RegisterBean data) {

                        list.clear();
                        address_list();
                    }
                });
    }

    @Override
    protected void onStart() {
        super.onStart();
        list.clear();
        address_list();
    }

    private void address_list() {
        EasyHttp.post(this)
                .api(new AddressListApi().setPage(mPage).setUserid(SPUtil.get(this, IntentKey.USERID,0)))
                .request(new HttpCallback<AddressBean>(this) {

                    @Override
                    public void onSucceed(AddressBean data) {
                        flag = data.getFlag();
                        list.addAll(data.getData());
                        adapter.setNewData(list);
                        adapter.notifyDataSetChanged();
                        addrNum.setText("共有"+list.size()+"个地址");

                    }
                });
    }

    @Override
    protected void initData() {


    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}